<?php
/**
 * Create by Phpstorm
 * User: Peco
 * Date: 2019/11/20
 * Time: 11:10
 * BannerController.php
 */

namespace App\Http\Controllers\Api\Shop;


use App\Http\Controllers\Api\BaseAdminController;
use App\Http\Requests\AuctionRule\AddAuctionRuleRequest;
use App\Http\Requests\AuctionRule\DeleteAuctionRuleRequest;
use App\Http\Requests\AuctionRule\GetAuctionRulePageRequest;
use App\Http\Requests\AuctionRule\UpdateAuctionRuleRequest;
use App\Http\Transformers\AuctionRule\GetAuctionRulePageTransformer;
use App\Repositories\AuctionRuleRepository;

/**
 * Class AuctionRuleController
 * @package App\Http\Controllers\Api\Shop
 */
class AuctionRuleController extends BaseAdminController
{
    /**
     * @var AuctionRuleRepository 注入的数据层
     */
    protected $auctionRuleRepository;

    /**
     * AuctionRuleController constructor.
     * @param AuctionRuleRepository $bannerRepository
     */
    public function __construct(AuctionRuleRepository $auctionRuleRepository)
    {
        $this->auctionRuleRepository = $auctionRuleRepository;
    }

    /**
     * 获取rule分页列表
     * @param GetAuctionRulePageRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function getAuctionRulePage(GetAuctionRulePageRequest $request){
        $limit = $request->get('limit');
        $data = $request->except(['limit', 'page']);
        $auctionRule = $this->auctionRuleRepository->getAuctionRulePageRepository($data, $limit);
        return $this->response->paginator($auctionRule, new GetAuctionRulePageTransformer());
    }

    /**
     *软删除规则
     * @param DeleteAuctionRuleRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function deleteAuctionRuleById(DeleteAuctionRuleRequest $request){
        $id = $request->get('id');
        if($this->auctionRuleRepository->deleteAuctionRuleByIdRepository($id)) {
            return $this->response->accepted('', 'success');
        }
        $this->response->errorInternal('删除失败！');
    }

    /**
     * 更新规则
     * @param UpdateAuctionRuleRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function updateAuctionRule(UpdateAuctionRuleRequest $request){
        if( !$data = $request->all() ){
            $this->response->errorBadRequest('缺少更新参数');
        }
        $result=$this->auctionRuleRepository->updateAuctionRuleRepository($data);
        if($result)
        {
            return $this->response->accepted('', 'success');
        }else
        {
            $this->response->errorInternal('更新失败！');
        }

    }

    /**
     * 更新规则状态
     * @param UpdateAuctionRuleRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function updateAuctionRuleStatus(UpdateAuctionRuleRequest $request){
        if( !$data = $request->all() ){
            $this->response->errorBadRequest('缺少更新参数');
        }
        $result=$this->auctionRuleRepository->updateAuctionRuleStatusRepository($data);
        if($result)
        {
            return $this->response->accepted('', 'success');
        }else
        {
            $this->response->errorInternal('更新失败！');
        }
    }

    /**
     * 添加拍卖规则
     * @param AddAuctionRuleRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function addAuctionRule(AddAuctionRuleRequest $request){
        $data = $request->all();
        $auctionRule=$this->auctionRuleRepository->getAuctionRuleByKeyRepository($data['ruleKey']);
        if(isset($auctionRule)){
            $this->response->errorBadRequest('ruleKey已存在');
        }
        $add = $this->auctionRuleRepository->addAuctionRuleRepository($data);
        if($add)
        {
            return $this->response->accepted('', 'success');
        }else
        {
            $this->response->errorInternal('添加失败！');
        }
    }
}
